import './index.less';
import Utils from "../../../common/utils.js";
// 弹窗
import Dialog from '../../../components/h5/dialog';
// 主页
import galleryListView from "./index.html";
import headerPartial from "../index/partial/header.html";


// 接口服务
import LoginService from "../../../services/loginService.js";
const loginService = new LoginService();

export default class GalleryList {
    constructor() {
        this.beginScroll = true;
        this.request = true;
        this.galleryContainer = $(".gallery-container");
        this.complied = _.template(galleryListView);
        this.compliedHeaderPartial = _.template(headerPartial);
        this.token = Utils.GetToken();
        console.log("token2是", this.token)
        if (this.token == '') {
            alert("无法获取token")
        }
        this.id = ''
        this.render();

    }
    //渲染页面
    async render() {
        let _this = this;
        var data2 = {};
        //加载header
        let headerHtml = _this.compliedHeaderPartial({
            data: 4,
            token: _this.token
        });

        //获取userid
        _this.id = jwt_decode(_this.token).UserId
        console.log('_this.id ', _this.id)

        //获取个人信息
        let user_result = await loginService.getUser(_this.token, _this.id);
        console.log("user_result", user_result)
        if (user_result.code == 200) {
            data2 = user_result.data;
        } else {
            alert("获取个人信息失败，请重试")
        }
        // //整个页面列表
        let galleryListHtml = this.complied({
            data2: data2
        });
        $(".content").html(galleryListHtml);

        this.registerEvents();

        setTimeout(() => {
            $(".header-container").html(headerHtml);
        }, 20);




    }

    //绑定事件
    registerEvents() {
        let _this = this;
        // 切换填写信息
        HandleChange = (value) => {
            $(".content-wrap-info").hide();
            $(".content-wrap-tab").show();
            $(`#sex-v option[value=${value}]`).prop("selected", true);
        }

        // 我的收藏
        onCollection = function() {
            console.log('我的收藏');
            window.location.href = `/collect.html?token=${_this.token}`;
        }

        // 退出登录
        HandleLogout = () => {
            console.log('退出登录 ~。~');
        }

        // $('#uploadfile').change(function(e) {
        //     e.preventDefault();
        //     var fileObj = document.getElementById("uploadfile").files[0]; // js 获取文件对象
        //     var re = new FileReader();
        //     re.readAsDataURL(fileObj);
        //     re.onload = function(re) {
        //         $('.content-wrap-tab .conent-img-upload img').attr("src", re.target.result);
        //     }
        // });

        // 提交信息
        HandleUpload = async () => {
            var NickName = $('#nink-v').val();
            var sex = $('#sex-v').val();
            var phone = $('#phone-v').val();
            //校检昵称
            var len = 0;
            for (var i = 0; i < NickName.length; i++) {
                var c = NickName.charCodeAt(i);
                if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
                    len++;
                } else {
                    len += 2;
                }
            }
            if (len > 16 || len < 1) {
                alert("昵称长度不符合要求")
                return;
            }
            //校检手机号
            var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
            if (!myreg.test(phone)) {
                alert("请填写真实手机号")
                return;
            }
            console.log(NickName, sex, phone, len)

            //获取个人信息
            let save_result = await loginService.saveUser(_this.token, _this.id,JSON.stringify({
                "NickName": NickName,
                "sex": parseInt(sex),
                "phone": phone
            }));
            console.log("save_result", save_result)
            if (save_result.code == 200) {
                window.location.reload();
            } else {
                alert("保存个人信息失败，请重试")
            }
        }

    }
}